package org.dy.db.support;

import java.util.Date;

import com.opensymphony.oscache.base.NeedsRefreshException;
import com.opensymphony.oscache.general.GeneralCacheAdministrator;

public class BaseCache extends GeneralCacheAdministrator {
	private int refreshPeriod;

	private static final long serialVersionUID = -4397192926052141162L;

	public BaseCache(int refreshPeriod) {
		super();
		this.refreshPeriod = refreshPeriod;
	}

	// 添加被缓存的对象;
	public void put(String key, Object value) {
		this.putInCache(key, value);
	}

	// 删除被缓存的对象;
	public void remove(String key) {
		this.flushEntry(key);
	}

	// 删除所有被缓存的对象;
	public void removeAll(Date date) {
		this.flushAll(date);
	}

	public void removeAll() {
		this.flushAll();
	}

	// 获取被缓存的对象;
	public Object get(String key) {
		try {
			if(this.refreshPeriod > 0)
				return this.getFromCache(key, this.refreshPeriod);
			else
				return this.getFromCache(key);
		} catch (NeedsRefreshException e) {
			this.cancelUpdate(key);
			return null;
		}

	}

}